package com.oa.modules.approvalsec1.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.entity.AudioVideoCollectEntity;
import com.oa.modules.approvalsec1.entity.AudioVideoEntity;
import com.oa.modules.approvalsec1.service.AudioVideoCollectService;
import com.oa.modules.approvalsec1.service.AudioVideoService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 视频教学-视频表
 *
 * @author jiangwei
 * @date 2021-03-16 09:47:45
 */
@RestController
@Api(tags = "视频相关接口")
@RequestMapping("audioVideo")
public class AudioVideoController extends AbstractController {
    @Autowired
    private AudioVideoService audioVideoService;

    @Autowired
    private AudioVideoCollectService audioVideoCollectService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = audioVideoService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息详情")
    public R info(@PathVariable("id") String id) {
        AudioVideoEntity audioVideo = audioVideoService.getById(id);
        QueryWrapper<AudioVideoCollectEntity> wra = new QueryWrapper<AudioVideoCollectEntity>().eq("AUDIO_VIDEO_ID", audioVideo.getId()).eq("CREATE_OPER", audioVideo.getCreateOper());
        AudioVideoCollectEntity a = audioVideoCollectService.getOne(wra);
        if (a == null || "".equals(a.getId()) || a.getId() == null) {
            return R.ok().put("audioVideo", audioVideo).put("flay", false);
        }
        return R.ok().put("audioVideo", audioVideo).put("flay", true);
    }

    @GetMapping("/wxinfo")
    @ApiOperation(value = "信息详情")
    public R wxinfo(@RequestParam("id") String id,@RequestParam("userId") String userId) {
        AudioVideoEntity audioVideo = audioVideoService.getById(id);
        QueryWrapper<AudioVideoCollectEntity> wra = new QueryWrapper<AudioVideoCollectEntity>().eq("AUDIO_VIDEO_ID", audioVideo.getId()).eq("CREATE_OPER", userId);
        AudioVideoCollectEntity a = audioVideoCollectService.getOne(wra);
        if (a == null || "".equals(a.getId()) || a.getId() == null) {
            return R.ok().put("audioVideo", audioVideo).put("flay", false);
        }
        return R.ok().put("audioVideo", audioVideo).put("flay", true);
    }



    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody AudioVideoEntity audioVideo) {
        audioVideo.setId(AppUtils.getUUID());
        audioVideo.setCreateDate(new Date());
        audioVideo.setCreateOper(String.valueOf(getUser().getUserId()));
        audioVideoService.save(audioVideo);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody AudioVideoEntity audioVideo) {
        audioVideo.setCreateDate(new Date());
        audioVideoService.updateById(audioVideo);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids) {
        audioVideoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
